#include "../Muzzy A Wrappers/D3DManager.h"
#include "../Muzzy A Wrappers/TextureManager.h"
#include "../Muzzy A Wrappers/DirectInput.h"
#include "../Muzzy A Wrappers/SoundManager.h"
#include "../Muzzy A Wrappers/Timer.h"
#include "../Muzzy A Wrappers/Camera.h"
#include "../Muzzy A Wrappers/Math.h"
#include "GamePlayState.h"
#include "Game.h"
#include "GameCursor.h"
#include "../MessageSystem.h"
#include "../Enums.h"
#include "../IBaseMessage.h"
#include "../Renderer.h"
#include "../Shaders.h"
#include "../ImageManager.h"
#include "../ObjectManager.h"

#define	LoadTexture(szFilePath,ppTexture) D3DXCreateTextureFromFileA(D3DManager::getInstance()->getDevice(),szFilePath,ppTexture);


GamePlayState::GamePlayState(void)
{
	d3dManager		= nullptr;
	input			= nullptr;
	soundManager	= nullptr;
	textureManager	= nullptr;
	pGame			= nullptr;
	time			= nullptr;
	cam				= nullptr;
	cursor			= nullptr;
	renderer		= nullptr;
	objManager		= nullptr;
}


GamePlayState::~GamePlayState(void)
{
}

GamePlayState* GamePlayState::getInstance()
{
	static GamePlayState instance;
	return &instance;
}

void GamePlayState::Enter()
{
	//////////////////////////////////////////////////////////////////////////
	// Get direct access to the singletons
	d3dManager		= D3DManager::getInstance();
	textureManager	= TextureManager::getInstance();
	soundManager	= SoundManager::getInstance();
	input			= DirectInput::getInstance();
	time			= Timer::getInstance();
	cam				= Camera::getInstance();
	cursor			= GameCursor::getInstance();
	renderer		= Renderer::getInstance();
	objManager		= ObjectManager::getInstance();

	// Initialize some things
	input->Clear();
	MessageSystem::getInstance()->setMessageProcFunc(&MessageProc);
	//////////////////////////////////////////////////////////////////////////

	// Start here...
	Shaders::getInstance()->Initialize();

	IDirect3DTexture9 *ptex;
	LoadTexture("Assets/Textures/hello.png",&ptex);
	g = ImageManager::getInstance()->CreateGeometryFromTexture(ptex);
	g1 = ImageManager::getInstance()->CreateGeometryFromTexture(ptex);
	TranslateMatrix(m,0,0,0);

	RECT rect = { 64,64,128,128 };
	g->setSrcRect(rect);
}

void GamePlayState::Exit()
{
	//////////////////////////////////////////////////////////////////////////
	// MEMORY CLEAN UP
	ImageManager::getInstance()->ReleaseGeometry();
	ObjectManager::getInstance()->Clear();
	Renderer::getInstance()->ClearQueue();
	Shaders::getInstance()->Shutdown();
	MessageSystem::getInstance()->ClearMessages();

	//////////////////////////////////////////////////////////////////////////
	// Set all the pointers to safe values
	d3dManager		= nullptr;
	input			= nullptr;
	soundManager	= nullptr;
	textureManager	= nullptr;
	pGame			= nullptr;
	time			= nullptr;
	cam				= nullptr;
}

void GamePlayState::Render()
{
	g1->Render(m);
	g->Render(m);
}

void GamePlayState::Update()
{
	MessageSystem::getInstance()->ProcessMessages();
}

void GamePlayState::Input()
{
	if(input->KeyDown(keyUp)) m._42 += .001f;
	if(input->KeyDown(keyLeft)) m._41 -= .001f;
	if(input->KeyDown(keyRight)) m._41 += .001f;
	if(input->KeyDown(keyDown)) m._42 -= .001f;
}

void GamePlayState::MessageProc( MessageID msg )
{
	// Handle all messages
	switch(msg)
	{
	case MSG_ALLPLAYERSDEAD:
		break;
	case MSG_PLAYERDEAD:
		break;
	default:
		break;
	}
}
